﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PersonalWebSite.Common
{
    public static class DateTimeExtensions
    {
        /// <summary>
        /// Tries to parse the given string as a DateTime struct.
        /// If the parsing fails the returned value is the given fallback value.
        /// </summary>
        /// <param name="value">String to try and parse</param>
        /// <param name="fallbackValue">Fallback value to return if parsing fails</param>
        /// <returns>The parsed DateTime or fallback value</returns>
        public static DateTime ParseAndFallback(string value, DateTime fallbackValue)
        {
            DateTime dateTime = fallbackValue;
            DateTime.TryParse(value, out dateTime);
            return dateTime;
        }

        /// <summary>
        /// Tries to parse the given string as a DateTime struct.
        /// If the parsing fails the returned value is DateTime.Now.
        /// </summary>
        /// <param name="value">String to try and parse</param>
        /// <returns>The parsed DateTime or DateTime.Now</returns>
        public static DateTime ParseAndFallback(string value)
        {
            return ParseAndFallback(value, DateTime.Now);
        }
    }
}